const jwt = require('jsonwebtoken')
const {secret} = require('../config/config')
// console.log(secret);
module.exports = (req, res, next) => {
  const token = req.get('token')
  console.log(token);
  if (!token) {
    return res.json({
      code: '2003',
      msg: 'token缺失',
      data: null
    })
  }

  jwt.verify(token, secret, (err, data) => {
    if (err) {
      return res.json({
        code: '2004',
        msg: 'token校验失败',
        data: null
      })
    }
    // 存入用户信息，以便查找用户的账本信息
    req.user = data
    next()
  })
  
}